﻿package Graficos 
{
	
	import flash.display.*;
	import flash.events.*;
	
	public class Grafico {
		
		static public var MAX_X:int = 128;
		static public var MAX_Y:int = 128;
		
		protected var bitmap:Bitmap;
		
		var difusaTextura:BitmapData;
		var sombraTextura:BitmapData;			

		
		public function Grafico()
		{
			difusaTextura = new BDDifuso5(1,1);
			sombraTextura = new BDShadow5(1,1);
			bitmap = new Bitmap(new BitmapData(MAX_X,MAX_Y));
			bitmap.addEventListener(Event.ENTER_FRAME,animar);
		}
		
		public function agregarEscenario(unEscenario:Sprite)
		{
			unEscenario.addChild(bitmap);
		}
		
		public function animar (e:Event)
		{
			//bitmap.x++;
			paint();
		}
		
		public function paint()
		{
			var currentBitmapData:BitmapData = bitmap.bitmapData;
			var xx,yy:uint;
			var colorSombra:uint = 0;
			var colorDifuso:uint = 0;
			var r,g,b:uint;
			for (yy=0;yy<MAX_Y;yy++)
			{
				for (xx=0;xx<MAX_X;xx++)
				{
					colorSombra = getSombraTexturaPixel(xx,yy);
					colorSombra = colorSombra & 255;
					colorDifuso = difusaTextura.getPixel(xx,yy);
					//----
					r = colorDifuso & 255;
					g = (colorDifuso >> 8) & 255;
					b = (colorDifuso >> 16) & 255;
					
					r = (r*colorSombra*1.2) >> 8; // AUMENTADO ROJO!
					g = (g*colorSombra) >> 8;
					b = (b*colorSombra) >> 8;
					//----
					
					colorDifuso = r | (g << 8) | (b << 16);
					currentBitmapData.setPixel(xx,yy,colorDifuso);
				}
			}
		}
		
		
		public function setCoords (xx:Number, yy:Number)
		{
			bitmap.x=xx;
			bitmap.y=yy;
		}
		
		protected function getSombraTexturaPixel(xx:Number, yy:Number):uint
		{
			return sombraTextura.getPixel(xx,yy);
		}
		
		public function position():Punto3d
		{
			return new Punto3d(bitmap.x,bitmap.y,0);
		}
		
	}
	
}